
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.PhoneBooks.Books;
using Yozeev.PhoneBooks.Books.Dtos;
using Yozeev.PhoneBooks.Books.DomainService;
using Yozeev.PhoneBooks.Books.Authorization;


namespace Yozeev.PhoneBooks.Books
{
    /// <summary>
    /// Books应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class BooksAppService : YozeevAppServiceBase, IBooksAppService
    {
        private readonly IRepository<Book, long> _bookRepository;

        private readonly IBookManager _bookManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public BooksAppService(IRepository<Book, long> bookRepository ,IBookManager bookManager)
        {
            _bookRepository = bookRepository; 
            _bookManager = bookManager;
        }


        /// <summary>
        /// 获取Book的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(BooksPermissions.BookPermissions.Query)] 
        public async Task<PagedResultDto<BookListDto>> GetPagedBook(GetBooksInput input)
        {

            var query = _bookRepository.GetAll();
            // TODO:根据传入的参数添加过滤条件
            

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            // var entityListDtos = ObjectMapper.Map<List<BookListDto>>(entityList);
            var entityListDtos = entityList.MapTo<List<BookListDto>>();

            return new PagedResultDto<BookListDto>(count,entityListDtos);
        }


        /// <summary>
        /// 通过指定id获取BookListDto信息
        /// </summary>
        [AbpAuthorize(BooksPermissions.BookPermissions.Query)] 
        public async Task<BookListDto> GetBookById(EntityDto<long> input)
        {
            var entity = await _bookRepository.GetAsync(input.Id);

            return entity.MapTo<BookListDto>();
        }

        /// <summary>
        /// 获取编辑 Book
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(BooksPermissions.BookPermissions.Create, BooksPermissions.BookPermissions.Edit)]
        public async Task<GetBookForEditOutput> GetBookForEdit(NullableIdDto<long> input)
        {
            var output = new GetBookForEditOutput();
            BookEditDto editDto;

            if (input.Id.HasValue)
            {
                var entity = await _bookRepository.GetAsync(input.Id.Value);

                editDto = entity.MapTo<BookEditDto>();

                //bookEditDto = ObjectMapper.Map<List<bookEditDto>>(entity);
            }
            else
            {
                editDto = new BookEditDto();
            }

            output.Book = editDto;
            return output;
        }


        /// <summary>
        /// 添加或者修改Book的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(BooksPermissions.BookPermissions.Create, BooksPermissions.BookPermissions.Edit)]
        public async Task CreateOrUpdateBook(CreateOrUpdateBookInput input)
        {

            if (input.Book.Id.HasValue)
            {
                await UpdateBook(input.Book);
            }
            else
            {
                await CreateBook(input.Book);
            }
        }


        /// <summary>
        /// 新增Book
        /// </summary>
        [AbpAuthorize(BooksPermissions.BookPermissions.Create)]
        protected virtual async Task<BookEditDto> CreateBook(BookEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <Book>(input);
            var entity=input.MapTo<Book>();
            

            entity = await _bookRepository.InsertAsync(entity);
            return entity.MapTo<BookEditDto>();
        }

        /// <summary>
        /// 编辑Book
        /// </summary>
        [AbpAuthorize(BooksPermissions.BookPermissions.Edit)]
        protected virtual async Task UpdateBook(BookEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _bookRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            // ObjectMapper.Map(input, entity);
            await _bookRepository.UpdateAsync(entity);
        }



        /// <summary>
        /// 删除Book信息的方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(BooksPermissions.BookPermissions.Delete)]
        public async Task DeleteBook(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _bookRepository.DeleteAsync(input.Id);
        }



        /// <summary>
        /// 批量删除Book的方法
        /// </summary>
        [AbpAuthorize(BooksPermissions.BookPermissions.BatchDelete)]
        public async Task BatchDeleteBook(List<long> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _bookRepository.DeleteAsync(s => input.Contains(s.Id));
        }

		
		//// custom codes
		// TODO 自定义代码

		// TODO 自定义代码

		// TODO 自定义代码

		// TODO 自定义代码

		
		
		
		//// custom codes end

        /// <summary>
        /// 导出Book为excel表,等待开发。
        /// </summary>
        /// <returns></returns>
        //public async Task<FileDto> GetBookToExcel()
        //{
        //	var users = await UserManager.Users.ToListAsync();
        //	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
        //	await FillRoleNames(userListDtos);
        //	return _userListExcelExporter.ExportToFile(userListDtos);
        //}

    }
}


